<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div>
        <h1>The Ajax 02 Page</h1>
        <button onclick="doAjaxGet()">Do Ajax Get</button>
        <button onclick="doAjaxPost()">Do Ajax Post</button>
        <button onclick="doAjaxDelete()">Do Ajax Delete</button>
        <button onclick="doAjaxPut()">Do Ajax Put</button>
    </div>
    <div id="result">
        <table>
            <thead>
                <tr>
                    <th>id</th>
                    <th>name</th>
                    <th>remark</th>
                </tr>
            </thead>
            <tbody>
                <tr th:each="brand:${data}">
                    <td th:text="${brand.id}"></td>
                    <td th:text="${brand.name}"></td>
                    <td th:text="${brand.remark}"></td>
                </tr>
            </tbody>
        </table>
    </div>
    <form enctype="application/x-www-form-urlencoded"></form>
    <script>
        function doAjaxGet() {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if(xhr.readyState==4 && xhr.status==200) {
                    document.getElementById("result").innerHTML = xhr.responseText;
                }
            }
            xhr.open("GET", "http://localhost/doAjaxGet", true);
            xhr.send();
        }

        function doAjaxPost() {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if(xhr.readyState==4 && xhr.status==200) {
                    document.getElementById("result").innerHTML = xhr.responseText;
                }
            }
            xhr.open("POST", "http://localhost/doAjaxPost", true);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhr.send("id=200&name=小米&remark=小米之家");
        }

        function doAjaxDelete() {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if(xhr.readyState==4 && xhr.status==200) {
                    document.getElementById("result").innerHTML = xhr.responseText;
                }
            }
            xhr.open("DELETE", "http://localhost/doAjaxDelete/100", true);
            xhr.send();
        }

        // 通过put请求更新数据
        function doAjaxPut() {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if(xhr.readyState==4 && xhr.status==200) {
                    document.getElementById("result").innerHTML = xhr.responseText;
                }
            }
            xhr.open("PUT", "http://localhost/doAjaxPut", true);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhr.send("id=200&name=oppo&remark=nice");
        }
    </script>
</body>
</html>